package scau.hotel.hotel_web.controller;


import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import scau.hotel.hotel_service.entity.Book;
import scau.hotel.hotel_service.entity.RoomType;
import scau.hotel.hotel_service.service.BookService;
import scau.hotel.hotel_service.service.RoomTypeService;
import scau.hotel.hotel_service.vo.BookVo;
import scau.hotel.hotel_model.controller.RespBean;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

/**
 * (Book)表控制层
 *
 * @author makejava
 * @since 2022-04-22 16:11:25
 */
@RestController
@RequestMapping("book")
@Api(value = "typeApi", tags = "预定控制器")
public class BookController {
    /**
     * 服务对象
     */
    @Resource
    private BookService bookService;

    /**
     * 服务对象
     */
    @Resource
    private RoomTypeService roomTypeService;

    /**
     * 不分页查询
     *
     * @param book 筛选条件
     * @return 查询结果
     */
    @GetMapping("/list")
    @ApiOperation("不分页查询所有")
    public RespBean queryAll(Book book) {
        return RespBean.success(this.bookService.queryAll(book));
    }

    /**
     * 分页查询
     *
     * @param pageNum  起始页
     * @param pageSize 每页数据大小
     * @param book     筛选条件
     * @return 查询结果
     */
    @GetMapping("/page")
    @ApiOperation("分页查询所有数据")
    public RespBean page(@RequestParam("pageNum") Integer pageNum, @RequestParam("pageSize") Integer pageSize, Book book) {
        PageHelper.startPage(pageNum, pageSize);
        List<Book> list = bookService.queryAll(book);
        PageInfo<List> pageInfo = new PageInfo(list);
        return RespBean.success(pageInfo);
    }

    /**
     * 通过主键查询单条数据
     *
     * @param bookId 主键
     * @return 单条数据
     */
    @GetMapping("{bookId}")
    @ApiOperation("根据id查询数据")
    public RespBean queryById(@PathVariable("bookId") Integer bookId) {
        return RespBean.success(this.bookService.queryById(bookId));
    }

    /**
     * 小程序新增数据
     *
     * @param json 实体
     * @return 新增结果
     */
    @PostMapping
    @ApiOperation("小程序端新增数据")
    public RespBean add(@RequestBody JSONObject json) {
        Map<Object, Object> map = (Map<Object, Object>) json.get("book");
        return this.bookService.insert(map);
    }

    /**
     * web端新增数据
     *
     * @param book 实体
     * @return 新增结果
     */
    @PostMapping("/addByWeb")
    @ApiOperation("web端新增数据")
    @PreAuthorize("hasAnyAuthority('admin','normal')")
    public RespBean addByWeb(@RequestBody @Validated Book book) {
        return this.bookService.insertByWeb(book);
    }

    /**
     * 编辑数据
     *
     * @param book 实体
     * @return 编辑结果
     */
    @PutMapping
    @ApiOperation("编辑数据")
    public RespBean edit(Book book) {
        return this.bookService.update(book);
    }

    /**
     * 删除数据
     *
     * @param bookId 主键
     * @return 删除是否成功
     */
    @DeleteMapping("/delete/{bookId}")
    @ApiOperation("删除数据")
    public RespBean deleteById(@PathVariable Integer bookId) {
        Book book = this.bookService.queryById(bookId);
        book.setDelFlag(0);
        return this.bookService.update(book);
    }

    /**
     * 不分页根据入住人名字模糊查询
     *
     * @param cusName 筛选条件
     * @return 查询结果
     */
    @GetMapping("/queryAllByCusName/{cusName}")
    @ApiOperation("不分页根据入住人名字模糊查询")
    public RespBean queryAllByCusName(@PathVariable("cusName") String cusName) {
        Book book = new Book();
        book.setCusName(cusName);
        return RespBean.success(this.bookService.queryAll(book));
    }

    /**
     * 小程序不分页查询房型数据
     *
     * @param roomType 筛选条件
     * @return 查询结果
     */
    @GetMapping("/listRoomType")
    @ApiOperation("小程序不分页查询房型数据")
    public RespBean queryAll(RoomType roomType) {
        return RespBean.success(this.roomTypeService.queryAll(roomType));
    }

    /**
     * 小程序不分页查询房型数据
     *
     * @param json 筛选条件
     * @return 查询结果
     */
    @PostMapping("/listRoomTypeAndGetPrice")
    @ApiOperation("小程序不分页查询房型数据")
    public RespBean queryAllAndGetPrice(@RequestBody JSONObject json) {
        Map<Object, Object> map = (Map<Object, Object>) json.get("book");
        return RespBean.success(this.bookService.queryAllAndGetPrice(map));
    }

    /**
     * 登记客户信息
     *
     * @param bookVo 客户信息数组
     * @return
     */
    @PostMapping("/autoDoRegistered")
    @ApiOperation("自动登记客户信息")
    @PreAuthorize("hasAnyAuthority('admin','normal')")
    public RespBean autoDoRegistered(@RequestBody BookVo bookVo) {
        return this.bookService.autoDoRegistered(bookVo);
    }
}

